package cn.wolfcode.shop.service.impl;

import cn.wolfcode.shop.domain.*;
import cn.wolfcode.shop.mapper.ProductMapper;
import cn.wolfcode.shop.mapper.ProductSkuMapper;
import cn.wolfcode.shop.mapper.SkuPropertyMapper;
import cn.wolfcode.shop.service.ICarService;
import cn.wolfcode.shop.util.RedisConstants;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CarServiceImpl implements ICarService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ProductSkuMapper productSKuMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private SkuPropertyMapper skuPropertyMapper;

    /**
     * 像购物车中添加货品
     * @param skuId
     * @param number
     * @param userLogin
     */
    @Override
    public void addCar(Long skuId, Integer number,UserLogin userLogin) {
        ProductSku productSku = productSKuMapper.selectByPrimaryKey(skuId);
        Product product = productMapper.selectByPrimaryKey(productSku.getProductId());
        //拼接一个key,并且通过该key去redis中获取数据,如果有数据,就在之前的量上添加上这次的量
        String carKey = RedisConstants.USER_CAR
                .replace("USERID",userLogin.getId()+"")
                .replace("PRODUCTID",productSku.getProductId()+"")
                .replace("SKUID",productSku.getId()+"");
        Map<String,Object> car = redisTemplate.opsForHash().entries(carKey);
        if(car != null && car.size()>0){
            Integer oldNumber = (Integer) redisTemplate.opsForHash().get(carKey, "number");
            redisTemplate.opsForHash().put(carKey,"number",oldNumber+number);
        }
        //否则,就在redis中新增一个购物车数据
        else{
            car = new HashMap<>();
            car.put("productId",productSku.getProductId());
            car.put("skuId",productSku.getId());
            car.put("number",number);
            car.put("price",productSku.getPrice());
            car.put("status",1);
            car.put("productName",product.getName());
            car.put("productImg",product.getImage());
            Map<String, String> propertys = new HashMap<>();
            List<ProductSkuProperty> productSkuPropertyList = productSku.getProductSkuPropertyList();
            for(ProductSkuProperty productSkuProperty:productSkuPropertyList){
                Long skuPropertyId = productSkuProperty.getSkuPropertyId();
                SkuProperty skuProperty = skuPropertyMapper.selectByPrimaryKey(skuPropertyId);
                String name = skuProperty.getName();
                propertys.put(name,productSkuProperty.getValue());
            }
            /*.forEach(productSkuProperty -> {
            propertys.put(productSkuProperty.getSkuProperty().getName(),productSkuProperty.getValue());*/
            //});
            car.put("propertys",propertys);
            car.put("select",true);
            redisTemplate.opsForHash().putAll(carKey,car);

        }
    }
}
